﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlantsVsZombies
{
    public class Level : InvisibleGameEntity
    {
        #region "Variable"
        private Vector2 _chacraterSize;
        private List<Plant> _plants; // danh sach cac plant duoc trong
        private int _nPlants = 0;
  
        private List<Plant> _plantTemplate;// danh sach plant mau se duoc chon
        private int _nPlantTemplate;
        private Plant _selectedPlant = null;

        private List<Ghost> _ghosts;
        private int _nGhost;

        private List<Vehicle> _vehicles;
        private int _nVehicles;

        public static  SpriteFont font;
        private string _levelName;
        private float _timer;

        private Boolean _gameOver = false;
        #endregion
        #region "Properties"
       

        public string LevelName
        {
            get { return _levelName; }
            set { _levelName = value; }
        }

        public bool GameOver
        {
            get { return _gameOver; }
        }

        #endregion
        #region "method"
        ContentManager content;
        public Level(int nGhost, List<Ghost> ghosts, ContentManager content, List<Vehicle> vehicles )
        {
            _plants = new List<Plant>();
            _plantTemplate = new List<Plant>();

            _ghosts = new List<Ghost>();
            _ghosts = ghosts;
            _nGhost = nGhost;
            _timer = 0f;
            _vehicles = vehicles;
            _nVehicles = _vehicles.Count;
            font = content.Load<SpriteFont>("SpriteFont1");

        }
       
        /*
         *   cac plant duoc dua vao trong khung de chon.
         */
        public void LoadPlantTemplete( Plant[] plants, int nPlants)
        {
            _nPlantTemplate = nPlants;
            for(int i=0; i<_nPlantTemplate; i++)
            {
                _plantTemplate.Add(plants[i]);
                for(int j =0; j<_plantTemplate[i].Sprites.Count; j++)
                {
                    _plantTemplate[i].Sprites[j].State = 0;
                }
            }
            _chacraterSize = plants[0].Size;
            
        }


     
        public void Update(GameTime gameTime)
        {
            // vi tri xuat hien cac plants
            InitTemplate();

            MouseState mouseState;

            // keo plant vao man hinh
            PullPlant(out mouseState);

            // di chuyen plant trong khi dang chon vi tri
            MovePlant(mouseState);

            // trong cay hoa xuong dat
            Planting(mouseState);

            // kiem tra viec Ghost an cac cay hoa.
            ZombieEatPlant(gameTime);

            // kiem tra viec hoa ban cac con ma.
            PlantAttackZombie(gameTime);

            // xe can zombie
            VehicleAttackZombie(gameTime);

            // kiem tra xem zombie da thang
            _gameOver = CheckGameOver();

        }

        private Boolean CheckGameOver()
        {
            for (int i = 0; i < _nGhost; i++)
                if (_ghosts[i].Position.X <= 0 && _ghosts[i].Visible)
                    return true;

            return false;
        }
        private void VehicleAttackZombie(GameTime gameTime)
        {
            
            for (int i = 0; i <_nVehicles; i++)
            {
                for (int j = 0; j < _nGhost; j++)
                {
                    
                    if (!_ghosts[j].IsCollisionVehicle(_vehicles[i])) 
                        continue;
                    _vehicles[i].IsMove = true;

                    
                }
                _vehicles[i].Update(gameTime);
            }
        }
        private void PlantAttackZombie(GameTime gameTime)
        {
            for (int i = 0; i < _nPlants; i++)
            {
                for (int j = 0; j < _nGhost; j++)
                {
                    _plants[i].Shoot(_ghosts[j], gameTime);
                }
                _plants[i].Update(gameTime);
            }
        }

        private void ZombieEatPlant(GameTime gameTime)
        {
            for (int i = 0; i < _nGhost; i++)
            {
                for (int j = 0; j < _nPlants; j++)
                {
                    if (_ghosts[i].IsCollisionPlant(_plants[j]))
                    {
                        _ghosts[i].EatPlant(_plants[j]);
                    }
                }
                _ghosts[i].Update(gameTime);
            }
        }

        private Vector2 Planting(MouseState mouseState)
        {
            Vector2 mouPos = new Vector2(mouseState.X, mouseState.Y);

            if (mouseState.LeftButton == ButtonState.Pressed &&
                _selectedPlant != null &&
                Map.IsAreaRegular(mouPos))

            {
                _selectedPlant.Position = Map.StandardizedPos(mouPos);
                for (int i = 0; i < _nPlants; i++)
                    if (_plants[i].Position == _selectedPlant.Position)
                        return mouPos;

                _plants.Add(_selectedPlant);
                _nPlants++;
                _selectedPlant = null;
            }
            return mouPos;
        }

        private void InitTemplate()
        {
            for (int i = 0; i < _nPlantTemplate; i++)
            {
                Vector2 locatiton = new Vector2(i*_plantTemplate[i].Size.X, 0);
                _plantTemplate[i].Position = locatiton;
                //_plantTemplate[i].Update(gameTime);
                _plantTemplate[i].Charging += 0.0625f;
            }
        }

        private void MovePlant(MouseState mouseState)
        {
            if (_selectedPlant != null)
            {
                _selectedPlant.Position = new Vector2(mouseState.X - _chacraterSize.X/2, mouseState.Y - _chacraterSize.Y/2);
            }
        }

        private void PullPlant(out MouseState mouseState)
        {
            mouseState = Mouse.GetState();
            if (mouseState.LeftButton == ButtonState.Pressed && _selectedPlant == null)
            {
                for (int i = 0; i < _nPlantTemplate; i++)
                {
                    if (_plantTemplate[i].IsClick(new Vector2(mouseState.X, mouseState.Y)))
                    {
                        if (_plantTemplate[i].Charging >= _plantTemplate[i].RechargeTime)
                        {
                            _selectedPlant = _plantTemplate[i].Clone();
                        }
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
           
            DrawPlant(spriteBatch);

            DrawTemplate(spriteBatch);

            if (_selectedPlant != null)
            {
                _selectedPlant.Draw(spriteBatch);
            }

            for (int i = 0; i < _nGhost; i++)
            {
                _ghosts[i].Draw(spriteBatch);
                
                spriteBatch.DrawString(font, _ghosts[i].Position.ToString(), new Vector2(400, 100+ 100 * i), Color.White);
            }

            for (int i = 0; i < _nVehicles; i++)
            {
                _vehicles[i].Draw(spriteBatch);
                spriteBatch.DrawString(font, _vehicles[i].Size.ToString(), new Vector2(100, 100 + 100 * i), Color.White);
            }
            
           
        }

        private void DrawTemplate(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nPlantTemplate; i++)
            {
                int n = _plantTemplate[i].NSprites;
                for (int j = 0; j < n; j++)
                    _plantTemplate[i].Sprites[j].Visible = true;

                Vector2 size = _plantTemplate[0].Size;
                _plantTemplate[i].Position = new Vector2(i*size.X, 0);

                for (int j = 0; j < _plantTemplate[i].Sprites.Count; j++)
                {
                    _plantTemplate[i].Sprites[j].State = 0;
                }

                _plantTemplate[i].Draw(spriteBatch);
            }
        }

        private void DrawPlant(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nPlants; i++)
            {
                for (int j = 0; j < _plants[i].Sprites.Count; j++)
                {
                    _plants[i].Sprites[j].State = -1;
                }
                _plants[i].Draw(spriteBatch);
            }
        }

        #endregion
      
    }
}

  
